import { Router } from 'express';
import { register, login, getCurrentUser, verifyEmail } from '../controllers/auth.controller';
import { authenticate } from '../middlewares/auth';
import { body } from 'express-validator';
import { validate } from '../middlewares/validator';

const router = Router();

// 注册验证规则
const registerValidation = [
  body('email').isEmail().withMessage('Invalid email format'),
  body('username').isLength({ min: 3, max: 20 }).withMessage('Username must be 3-20 characters'),
  body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters'),
];

// 登录验证规则
const loginValidation = [
  body('email').isEmail().withMessage('Invalid email format'),
  body('password').notEmpty().withMessage('Password is required'),
];

// 公开路由
router.post('/register', validate(registerValidation), register);
router.post('/login', validate(loginValidation), login);
router.get('/verify-email/:token', verifyEmail);

// 需要认证的路由
router.get('/me', authenticate, getCurrentUser);

export default router;
